package com.gongyibao.gbase.controller;

import com.gongyibao.gbase.common.BaseController;
import com.gongyibao.gbase.common.BaseResult;
import com.gongyibao.gbase.entity.TUser;
import com.gongyibao.gbase.service.TUserService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.Date;

@RestController
@RequestMapping("/api/user/")
public class UserController extends BaseController {

    @Resource
    TUserService userService;

    @GetMapping("getUserById")
    public BaseResult<TUser> getUserById(Integer id) {
        TUser tUser = userService.getUserById(id);
        return success(tUser);
    }

    @PostMapping("insert")
    public BaseResult<Boolean> insertUser(@RequestBody TUser user) {
        Boolean flag = userService.insertUser(user);
        return success(flag);
    }

    @PostMapping("insertUser")
    public BaseResult<Boolean> insertUser() {

        TUser user = getUserInstance();

        Boolean flag = userService.insertUser(user);
        return success(flag);
    }

    @PostMapping("updateUser")
    public BaseResult<Boolean> updateUser(@RequestBody TUser user) {

        Boolean flag = userService.updateUser(user);
        return success(flag);
    }

    @PostMapping("delUser")
    public BaseResult<Object> updateUser(Integer id) {

        Boolean flag = userService.delUser(id);
        if (flag) {
            return success(flag);
        } else {
            return fail("删除失败");
        }
    }


    private TUser getUserInstance() {
        TUser user = new TUser();
        user.setId(0);
        user.setName("testUser");
        user.setSex("1");
        user.setAge(Short.valueOf("20"));
        user.setSalary(new BigDecimal("999.99"));
        user.setProfile("ccccccccccccccc");

        Date dt = new Date();
        user.setDtYs(dt);
        user.setCurrentDate(dt);
        user.setDtYy(dt);
        user.setDtHs(dt);
        user.setDepId(1);
        user.setNameAlias("testUser_alias");
        return user;
    }


}
